var apiHost = wx.getStorageSync("apiHost");    //请求地址

Page({

  /**
   * 页面的初始数据
   */
  data: {
    skuDetailVos : [],           //规格参数 集合
    showTotleInflag : false,     //是否展示库存
    maxBugCount : '1',           //一个人最多购买数量
    totalIntentory : '',          //总库存     
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this ; 
    var collageId = wx.getStorageSync("collageId");        //拼团id
    var maxBugCount = wx.getStorageSync("maxBugCount");    //一个人最多购买数量
    
    //判断一人购买数量是否为空
    if (maxBugCount==""){
      maxBugCount = that.data.maxBugCount;
    }

    that.setData({
      collageId: collageId,
      maxBugCount: maxBugCount
    })
    that.initSeniotSetUp();   //initSeniotSetUp
  },

  /**
   * 一人最多购买数量
   */
  maxBugCountFf : function(e){
    var that = this ;
    var maxBugCount  = e.detail.value

    that.setData({
      maxBugCount: maxBugCount
    });
    wx.setStorageSync('maxBugCount', maxBugCount); 
  },

  /**
   * 初始化高级设置
   */
  initSeniotSetUp : function(){
    var that = this;

    var collageId = that.data.collageId;    //拼团id
    wx.showLoading({
      title: '玩命加载中',
      icon : 'loading',
      mask: true
    })

    wx.request({
      url: apiHost + 'hpp/collage/gjSkuDetailPage',
      method : 'get',
      header:{
        "content-type" : "application/json"
      },
      data : {
        collageId: collageId
      },
      success : function(res){   
        var code = res.data.code ; 
        var msg = res.data.msg;

        var skuDetailVos1 = '';
        var skuDetailVos2 = '';

        if ("10000" == code ){

          var skuDetailVos = res.data.data.skuDetailVos;      //总规格参数
          var showTotleInflag = that.data.showTotleInflag;    //判断库存是否显示

          var totalIntentory = res.data.data.totalIntentory;  //总库存
          wx.setStorageSync('totalIntentory', totalIntentory);//设置全局总库存

          //判断是否有规格参数
          if (skuDetailVos.length !=0){
            if (skuDetailVos.length ==1){
              skuDetailVos1 = skuDetailVos[0];       //规格参数1
            }
            if (skuDetailVos.length == 2) {
              skuDetailVos1 = skuDetailVos[0];       //规格参数1
              skuDetailVos2 = skuDetailVos[1];        //规格参数2 
            }
            showTotleInflag = true;
          }
          that.setData({
            skuDetailVos: skuDetailVos,
            skuDetailVos1: skuDetailVos1,
            skuDetailVos2: skuDetailVos2,
            showTotleInflag: showTotleInflag,
            totalIntentory: totalIntentory
          }) 
        }
        wx.hideLoading();
      },
      fail :function(e){
        wx.hideLoading();   //隐藏加载loading
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 2000,
          mask: true
        });
      }
    })
  },

  /**
   * 跳转编辑规格
   */
  goEditProperty : function(e){  
    var collageCategoryId = e.currentTarget.dataset.toggle;
    
    wx.reLaunch({
      url: '../editProperty/editProperty?collageCategoryId=' + collageCategoryId,
      success: function () {
        wx.hideLoading();   //隐藏加载loading
      },
    });
  },

  /**
   * 跳转 库存编辑
   */
  goEditInventory :function(){
    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    });

    wx.reLaunch({
      url: '../editInventory/editInventory',
      success: function () {
        wx.hideLoading();   //隐藏加载loading
      },
    });
  },
  /**
   * 高级设置的保存按钮
   */
  seniorSetUpSave : function(){
    var that = this;
    var maxBugCount = that.data.maxBugCount;        //一人最多购买次数
    var totalIntentory = that.data.totalIntentory;  //总库存
    var showTotleInflag = that.data.showTotleInflag;//判断是否存在规格

    wx.setStorageSync('maxBugCount', maxBugCount);
    wx.setStorageSync('totalIntentory', totalIntentory); 

    //高级设置校验
    if (maxBugCount==''){
      that.promptMsg("请填写单人购买数量");
      return;
    } else if (totalIntentory==''){
      that.promptMsg("请设置规格库存");
      return;
    } else if (!showTotleInflag){
      that.promptMsg("请选择规格");
      return;
    }
    that.goSecondOrdinaryCollage('../secondOrdinaryCollage/secondOrdinaryCollage');
  },
  /**
   * 跳转第二发团
   */
  goSecondOrdinaryCollage : function(url){
    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    });

    wx.reLaunch({
      url: url,
      success: function (e) {
        wx.hideLoading();   //隐藏加载loading    
      },
      fail : function(e){
        console.log(e)
      }
    });
  },
  /**
   * 提示信息
   */
  promptMsg: function (msg) {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 2000,
      mask: true
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  }
})